package Utils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import JDBC.DatabaseDao;

public class StatisticTool {
	// 含有key词汇的微博个数
	public static int getStatusHits(String key) {
		int result = 0;
		try {
			String sql = "select count(*) from statuses where content like '%"
					+ key + "%'";
			System.out.println(sql);
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			rs.next();
			result = rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// 含有key词汇集合的微博个数
	public static int getStatusHits(String[] keys) {
		int result = 0;
		try {
			Connection con = DatabaseDao.getConnection();
			for (int i = 0; i < keys.length; i++) {
				String sql = "select count(*) from statuses where content like '%"
						+ keys[i] + "%'";
				System.out.println(sql);
				Statement stmt = con.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				rs.next();
				result += rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// 某人含有key词汇的微博个数
	public static int getStatusHits(String key, String user_id) {
		int result = 0;
		try {
			String sql = "select count(*) from statuses where content like '%"
					+ key + "%' and user_id=" + user_id;
			System.out.println(sql);
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			rs.next();
			result = rs.getInt(1);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// 含有key词汇集的微博个数
	public static int getStatusHits(String[] keys, String user_id) {
		int result = 0;
		try {
			Connection con = DatabaseDao.getConnection();
			for (int i = 0; i < keys.length; i++) {
				String sql = "select count(*) from statuses where content like '%"
						+ keys[i] + "%' and user_id=" + user_id;
				System.out.println(sql);
				Statement stmt = con.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				rs.next();
				result += rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// 含有key词汇的微博
	public static ArrayList<String> getHitStatuses(String key) {
		ArrayList<String> result = new ArrayList<String>();
		try {
			String sql = "select status_id from statuses where content like '%"
					+ key + "%'";
			System.out.println(sql);
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				result.add(rs.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// 含有key词汇集的微博
	public static ArrayList<String> getHitStatuses(String[] keys) {
		ArrayList<String> result = new ArrayList<String>();
		try {
			Connection con = DatabaseDao.getConnection();
			for (int i = 0; i < keys.length; i++) {
				String sql = "select status_id from statuses where content like '%"
						+ keys[i] + "%'";
				System.out.println(sql);

				Statement stmt = con.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				while (rs.next()) {
					result.add(rs.getString(1));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// 某人含有key词汇的微博
	public static ArrayList<String> getHitStatuses(String key, String user_id) {
		ArrayList<String> result = new ArrayList<String>();
		try {
			String sql = "select status_id from statuses where content like '%"
					+ key + "%' and user_id=" + user_id;
			System.out.println(sql);
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				result.add(rs.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// 某人含有key词汇集的微博
	public static ArrayList<String> getHitStatuses(String[] keys, String user_id) {
		ArrayList<String> result = new ArrayList<String>();
		try {
			Connection con = DatabaseDao.getConnection();
			for (int i = 0; i < keys.length; i++) {
				String sql = "select status_id from statuses where content like '%"
						+ keys[i] + "%' and user_id=" + user_id;
				System.out.println(sql);
				Statement stmt = con.createStatement();
				ResultSet rs = stmt.executeQuery(sql);
				while (rs.next()) {
					result.add(rs.getString(1));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;
	}

	// key1和key2在语料库中的共现次数
	public static int getConcurrences(String key1, String key2, int scope) {
		int result = 0;
		Connection con = DatabaseDao.getConnection();
		// scope=0:status;scope=1:content;scope=2:status+content
		try {
			String sql = "";
			Statement stmt = con.createStatement();
			switch (scope) {
			case 0:
				sql = "select count(*) from statuses where content like'%"
						+ key1 + "%" + key2 + "%'";
				System.out.println(sql);

				break;
			case 1:
				sql = "select count(*) from comments where content like'%"
						+ key1 + "%" + key2 + "%'";
				System.out.println(sql);
				break;
			default:
				break;
			}
			ResultSet rs = stmt.executeQuery(sql);
			rs.next();
			result = rs.getInt(1);
			return result;
		} catch (Exception e) {
		}
		return result;
	}

	public static ArrayList<Long> getStatusIntervals(String user_id) {
		ArrayList<Long> result = new ArrayList<Long>();

		String last = null;
		String cur = null;
		try {
			Connection con = DatabaseDao.getConnection();
			String sql = "select created_at from statuses where user_id="
					+ user_id + " order by created_at asc";
			System.out.println(sql);
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			boolean isFirst = false;
			while (rs.next()) {
				if (isFirst == false)
					last = rs.getString(1);
				isFirst = true;
				cur = rs.getString(1);

				DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date curDate = df.parse(cur);
				Date lastDate = df.parse(last);
				long diff = curDate.getTime() - lastDate.getTime();
				long days = diff / (1000 * 60 * 60);// hours
				System.out.println("发帖时间差(小时):" + days);
				result.add(days);
				last = cur;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;

	}

	public static long str2Long(String timeStr) {
		long time = 0;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
			Date dt2 = sdf.parse(timeStr);
			time = dt2.getTime() / (1000 * 60 * 60);// 计算到
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	public static void main(String args[]) {
		 String keys[] = { "程序", "微软" };
		// int hits1 = getStatusHits("程序");
		// int hits2 = getStatusHits("程序", "1655722764");// 1655722764
		 int hits3 = getStatusHits(keys);
		// int hits4 = getStatusHits(keys, "1655722764");
		// ArrayList<String> hitStatuses1 = getHitStatuses("程序");
		// ArrayList<String> hitStatuses2 = getHitStatuses("程序", "1655722764");
		 ArrayList<String> hitStatuses3 = getHitStatuses(keys);
		// ArrayList<String> hitStatuses4 = getHitStatuses(keys, "1655722764");
		 System.out.println(hits3);
		 System.out.println(hitStatuses3);
		// int concurrences = getConcurrences("你", "的", 1);
		// System.out.println("共现:" + concurrences);

//		Hashtable<Long, Integer> interval_count = new Hashtable<Long, Integer>();
//		ArrayList<Long> intervals = getStatusIntervals("1671526850");
//		for (int i = 0; i < intervals.size(); i++) {
//			if (interval_count.containsKey(intervals.get(i)))
//				interval_count.put(intervals.get(i), interval_count
//						.get(intervals.get(i)) + 1);
//			else
//				interval_count.put(intervals.get(i), 1);
//		}
//		Printers.htPrinters(interval_count);
	}
}
